#include "BNormalised.h"
#include "ObjMeshManager.h"
#include "ScreenManager.h"

#include <iostream>

BNormalised::BNormalised()
{
	m_pos = D3DXVECTOR3(0.0f, -0.8f, -10.0f);
	m_text.SetFont("pixel_small", L"../../Assets/pixel2.png");
	
	m_screenSize.x = (float)TheScreenManager::Instance()->GetWindowWidth();
	m_screenSize.y = (float)TheScreenManager::Instance()->GetWindowHeight();

	m_outlineSize = 0.01f;	

	m_name = " Normalised";
	m_size.x = 11.0f * m_text.GetSize();
	m_size.y = m_text.GetSize() * 1.1f;
	m_size.z = 1.0f;
	m_mouseDidIntersect = false;
	m_normalised = false;
	m_mouseButtonWasReleased = false;

	TheEventPoller::Instance()->AddHandler(this);
}

BNormalised::~BNormalised()
{
	TheEventPoller::Instance()->RemoveHandler(this);
}

void BNormalised::Draw()
{	
	if (m_normalised)
	{
		TheObjMeshManager::Instance()->SetColour("button", D3DXVECTOR4(0.349f, 0.8901f, 0.019f, 1.0f));
	}
	else
	{
		TheObjMeshManager::Instance()->SetColour("button", D3DXVECTOR4(0.6156f, 0.9019f, 0.3411f, 1.0f));
	}
	
	TheObjMeshManager::Instance()->Translate("button", D3DXVECTOR3(m_pos.x * 0.5f, m_pos.y * 0.5f, m_pos.z));
	TheObjMeshManager::Instance()->Scale("button", m_size);
	TheObjMeshManager::Instance()->Draw("button");
	
	TheObjMeshManager::Instance()->SetColour("button", D3DXVECTOR4(0.0f, 0.0f, 0.0f, 1.0f));
	TheObjMeshManager::Instance()->Translate("button", D3DXVECTOR3(m_pos.x * 0.5f, m_pos.y * 0.5f, m_pos.z + 0.5f));
	TheObjMeshManager::Instance()->Scale("button", D3DXVECTOR3(m_size.x + m_outlineSize, 
																m_size.y + m_outlineSize, 
																m_size.z));
	TheObjMeshManager::Instance()->Draw("button");

	m_text.SetColour(D3DXVECTOR4(1.0f, 1.0f, 1.0f, 0.0f));
	m_text.Translate(D3DXVECTOR3(m_pos.x, m_pos.y, 0.0f));
	m_text.Update(m_name);
	m_text.Draw();
}

void BNormalised::Update()
{

}

void BNormalised::IntersectedMouse()
{
	D3DXVECTOR2 pos = D3DXVECTOR2(m_pos.x, m_pos.y);
	//pos.x += m_sizeSlider.x * 0.5f;

	float maxX = pos.x + m_size.x;
	float minX = pos.x - m_size.x;
	float maxY = pos.y + m_size.y;
	float minY = pos.y - m_size.y;
	
	m_mouseIntersected = false;
	if (m_convertedMousePos.x > minX && m_convertedMousePos.x < maxX)
	{
		if (m_convertedMousePos.y > minY && m_convertedMousePos.y < maxY)
		{
			m_mouseIntersected = true;
		}
	}	
}

void BNormalised::SetPos(const D3DXVECTOR3& pos)
{
	m_pos = pos;
}

void BNormalised::SetSound(Sound* s)
{
	m_sound = s;
}

void BNormalised::OnMouseMotionEvent(const LPARAM& event)
{
	int x = LOWORD(event);
	int y = HIWORD(event);
	m_mousePos = D3DXVECTOR2(float(x), float(y));
	m_convertedMousePos.x = (m_mousePos.x - (m_screenSize.x * 0.5f)) / (m_screenSize.x * 0.5f);
	m_convertedMousePos.y = (m_mousePos.y - (m_screenSize.y * 0.5f)) / (m_screenSize.y * 0.5f) * -1.0f;
	
	m_mouseMoved = true;
}

void BNormalised::OnMouseButtonDownEvent(const MSG& event)
{
	if (event.message == WM_LBUTTONDOWN)
	{
		m_mouseDown = true;
		m_mouseDownPos = D3DXVECTOR2((LOWORD(event.lParam)), (HIWORD(event.lParam)));
		m_convertedMouseDownPos.x = (m_mouseDownPos.x - (m_screenSize.x * 0.5f)) / (m_screenSize.x * 0.5f);
		m_convertedMouseDownPos.y = (m_mouseDownPos.y - (m_screenSize.y * 0.5f)) / (m_screenSize.y * 0.5f) * -1.0f;

		IntersectedMouse();
		if (m_mouseIntersected)
		{
			if (m_mouseDown)
			{
				if (m_normalised)
				{
					m_normalised = false;
				}
				else
				{
					m_normalised = true;
				}
				m_sound->SetNormalised(m_normalised);
			}
		}
	}
}

void BNormalised::OnMouseButtonUpEvent(const MSG& event)
{
	if (event.message == WM_LBUTTONUP)
	{
		m_mouseDown = false;

		if (m_mouseIntersected)
		{
			m_mouseDidIntersect = true;
		}

		m_mouseIntersected = false;
		m_mouseButtonWasReleased = true;
	}
}